<template>
  <div id="main">
    <h2>家长沟通记录</h2>
    <div class="head">
      <el-button
        type="primary"
        style="margin:20px"
        @click="handleOpen('添加记录', 1)"
      >添加记录</el-button>
      <el-input
        v-model="searchTableInfo"
        placeholder="请输入搜索内容"
        style="width:240px;float: right;margin:20px;"
      />

      <div id="table-area">
        <el-table
          v-loading="listLoading"
          :data="
            list.slice((currentPage - 1) * pagesize, currentPage * pagesize)
          "
          border
          fit
          highlight-current-row
        >
          <el-table-column label="序号" type="index" align="center">
            <template scope="scope">
              <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            prop="pstuName"
            label="学生姓名"
            width="120"
          />
          <el-table-column
            align="center"
            prop="pparentName"
            label="家长姓名"
            width="120"
          />
          <el-table-column
            align="center"
            prop="pstaffName"
            label="责任教师"
            width="120"
          />
          <el-table-column
            align="center"
            prop="pattendingDoctorName"
            label="主治医师"
            width="120"
          />
          <el-table-column
            align="center"
            prop="pstaffidDirectorName"
            label="教学主管"
            width="120"
          />
          <el-table-column align="center" prop="pmeetingPlaceName" label="会议地点" width="120" />
          <el-table-column align="center" prop="pCreattime" label="会议日期" />

          <el-table-column align="center" label="操作">
            <template slot-scope="Scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                @click="handleOpen('修改记录', 2, Scope.row.pcommId)"
              >编辑</el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(Scope.row.pcommId, Scope.row.pstuName)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div id="pages">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[5, 10]"
            :page-size="pagesize"
            layout="total, sizes,prev, pager, next"
            :total="list.length"
            prev-text="上一页"
            next-text="下一页"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>

    <el-dialog
      title=""
      :close-on-click-modal="false"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
    >
      <el-form
        ref="parentcomm"
        :model="parentcomm"
        label-position="top"
        :rules="rules"
      >
        <el-form-item
          prop="pstuid"
          label="学生姓名"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="parentcomm.pstuid"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in student"
              :key="item.studentId"
              :label="item.String"
              :value="item.studentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="pparentid"
          label="家长姓名"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="parentcomm.pparentid"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in parent"
              :key="item.parentId"
              :label="item.String"
              :value="item.parentId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="pparentRelation"
          label="儿童关系"
          :label-width="formLabelWidth"
          style="width: 40%;"
        >
          <el-input
            v-model="parentcomm.pparentRelation"
            autocomplete="off"
            maxlength="10"
            show-word-limit
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="pstaffid"
          label="责任老师"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="parentcomm.pstaffid"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in resteacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="pstaffidDirector"
          label="教导主任"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="parentcomm.pstaffidDirector"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in Dirteacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="pattendingdoctorId"
          label="主治医师"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="parentcomm.pattendingdoctorId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in teacher"
              :key="item.staffId"
              :label="item.String"
              :value="item.staffId"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          prop="pmeetingplaceId"
          label="会议地点"
          :label-width="formLabelWidth"
        >
          <el-select
            v-model="parentcomm.pmeetingplaceId"
            filterable
            placeholder="请选择"
            style="width: 40%;"
          >
            <el-option
              v-for="item in classroom"
              :key="item.roomId"
              :label="item.roomLoc"
              :value="item.roomId"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="pCreattime" label="会议时间">
          <div class="block">
            <el-date-picker
              v-model="parentcomm.pCreattime"
              type="datetime"
              placeholder="选择时间"
              format="yyyy -MM - dd  hh：mm :ss"
              value-format="yyyy-MM-dd hh:mm:ss"
              style="width:245px"
            />
          </div>
        </el-form-item>
        <el-form-item
          prop="pissue"
          label="事件描述"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="parentcomm.pissue"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="pcommInfo"
          label="沟通内容"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="parentcomm.pcommInfo"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="pparentInfo"
          label="家长问题及建议"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="parentcomm.pparentInfo"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item
          prop="pinfo"
          label="处理意见"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="parentcomm.pinfo"
            autocomplete="off"
            maxlength="200"
            show-word-limit
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 15 }"
            placeholder="请输入内容"
          />
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-if="editType == 2"
          type="primary"
          @click="handleUpdate('parentcomm')"
        >修 改</el-button>
        <el-button
          v-if="editType == 1"
          type="primary"
          @click="handleAddUser('parentcomm')"
        >添 加</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  updateParentComm,
  deleteParentComm,
  getAllParentComm,
  getParentCommbyId,
  addoneParentComm,
  getTeacherString,
  getAllstudentName,
  getAllClassRoom,
  getAllparentName
} from '@/api/parentcomm'
import { getAllTeacherString } from '@/api/supervision'
export default {
  data() {
    return {
      value: '',
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      tableList: [],
      searchTableInfo: '',
      parentcomm: {},
      parent: {},
      listLoading: false,
      dialogFormVisible: false,
      editType: 1, // 1 添加 2 修改
      student: {},
      resteacher: {},
      Dirteacher: {},
      teacher: {},
      classroom: {},
      search: '',
      formLabelWidth: '200px',
      rules: {
        pcommInfo: [
          { required: true, message: '沟通内容不能为空', trigger: 'blur' }
        ],
        pCreattime: [
          { required: true, message: '创建时间不能为空', trigger: 'blur' }
        ],
        pinfo: [
          { required: true, message: '处理建议不能为空', trigger: 'blur' }
        ],
        pparentInfo: [
          { required: true, message: '家长问题及建议不能为空', trigger: 'blur' }
        ],
        pissue: [
          { required: true, message: '事件描述不能为空', trigger: 'blur' }
        ],
        pparentRelation: [
          { required: true, message: '儿童关系不能为空', trigger: 'blur' }
        ],
        pmeetingplaceId: [
          { required: true, message: '会议地点不能为空', trigger: 'blur' }
        ],
        pstuid: [
          { required: true, message: '学生姓名不能为空', trigger: 'blur' }
        ],
        pstaffidDirector: [
          { required: true, message: '教学主管不能为空', trigger: 'blur' }
        ],
        pstaffid: [
          { required: true, message: '主责老师不能为空', trigger: 'blur' }
        ],
        pattendingdoctorId: [
          { required: true, message: '主治医师不能为空', trigger: 'blur' }
        ],
        pparentid: [
          { required: true, message: '家长姓名不能为空', trigger: 'blur' }
        ]
      },
      newParent: {
        pcommId: '',
        pstuName: '',
        pstaffidDirectorName: '',
        pstaffName: '',
        pCreattime: '',
        pparentName: '',
        pattendingDoctorName: '',
        pmeetingPlaceName: ''

      }
    }
  },
  computed: {
    // 根据计算属性模糊搜索
    list() {
      const searchTableInfo = this.searchTableInfo
      console.log(this.searchTableInfo)
      if (searchTableInfo) {
        // filter() 方法创建一个新的数组，新数组中的元素是通过检查指定数组中符合条件的所有元素。
        // 注意： filter() 不会对空数组进行检测。
        // 注意： filter() 不会改变原始数组。
        return this.tableList.filter(data => {
          console.log('success' + data)
          // some() 方法用于检测数组中的元素是否满足指定条件;
          // some() 方法会依次执行数组的每个元素：
          // 如果有一个元素满足条件，则表达式返回true , 剩余的元素不会再执行检测;
          // 如果没有满足条件的元素，则返回false。
          // 注意： some() 不会对空数组进行检测。
          // 注意： some() 不会改变原始数组。
          return Object.keys(data).some(key => {
            // indexOf() 返回某个指定的字符在某个字符串中首次出现的位置，如果没有找到就返回-1；
            // 该方法对大小写敏感！所以之前需要toLowerCase()方法将所有查询到内容变为小写。
            return (
              String(data[key])
                .toLowerCase()
                .indexOf(searchTableInfo) > -1
            )
          })
        })
      }
      return this.tableList
    }
  },
  created() {
    this.fetchData()
    this.getAllparentcommtudent()
    this.getResTeacher()
    this.getAlldirTeacher()
    this.getAllClassRooms()
    this.getAllparentNames()
    this.getAllTeacher()
  },
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    getAllparentcommtudent() {
      getAllstudentName().then(res => {
        this.student = res.obj
      })
    },
    getResTeacher() {
      getTeacherString(7).then(res => {
        this.resteacher = res.obj
      })
    },
    getAlldirTeacher() {
      getTeacherString(6).then(res => {
        this.Dirteacher = res.obj
      })
    },
    getAllTeacher() {
      getAllTeacherString().then(res => {
        this.teacher = res.obj
      })
    },

    getAllClassRooms() {
      getAllClassRoom().then(res => {
        this.classroom = res.obj
      })
    },
    getAllparentNames() {
      getAllparentName().then(res => {
        this.parent = res.obj
      })
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.tableList = []
      this.listLoading = true
      getAllParentComm().then(data => {
        console.log(data.obj)
        this.newParent
        for (var i = 0; i < data.obj.length; i++) {
          this.newParent.pcommId = data.obj[i].parentComm.pcommId
          this.newParent.pCreattime = data.obj[i].parentComm.pCreattime
          this.newParent.pstuName = data.obj[i].pstuName
          this.newParent.pstaffidDirectorName =
            data.obj[i].pstaffidDirectorName
          this.newParent.pstaffName = data.obj[i].pstaffName
          this.newParent.pparentName = data.obj[i].pparentName
          this.newParent.pattendingDoctorName =
            data.obj[i].pattendingDoctorName
          this.newParent.pmeetingPlaceName = data.obj[i].pmeetingPlaceName

          this.tableList.push(this.newParent)
          this.newParent = {}
        }
        console.log(this.tableList)
        this.listLoading = false
      })
    },
    handleDelete(id, username) {
      this.$confirm('确认删除该学生？姓名为' + username).then(() =>
        deleteParentComm(id).then(data => {
          this.$notify({
            title: '操作成功',
            type: 'success'
          })
          const totalPage = Math.ceil(
            (this.tableList.length - 1) / this.pagesize
          )
          const page =
            this.currentPage > totalPage ? totalPage : this.currentPage
          this.currentPage = this.currentPage < 1 ? 1 : page
          this.fetchData()
        })
      )
    },
    handleAddUser(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.parentcomm)
          addoneParentComm(this.parentcomm).then(resp => {
            this.$notify({
              title: '操作成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.parentcomm = {}
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    },
    handleOpen(title, type, id) {
      console.log(this.a)
      console.log(id)
      this.parentcomm = {}
      this.dialogFormVisible = true
      this.title = title
      this.editType = type
      if (type === 2) {
        getParentCommbyId(id).then(resp => {
          console.log(resp)
          this.parentcomm = resp.obj.parentComm
          console.log(this.parentcomm)
        })
      }
    },
    handleClose() {
      this.dialogFormVisible = false
      this.parentcomm = {}
    },

    handleUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateParentComm(this.parentcomm).then(resp => {
            this.$notify({
              title: '修改成功',
              type: 'success'
            })
            this.fetchData()
          })
          this.dialogFormVisible = false
          return true
        } else {
          this.$notify({
            title: '请正确填写',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
#main {
  margin-left: 5px;
  margin-right: 5px;
}
#table-area {
  margin-left: 10px;
  margin-right: 10px;
}
</style>
